package com.abocode.aichat.server.modules.handler;

import com.abocode.aichat.data.*;
import com.abocode.aichat.server.context.channel.ChannelWrapper;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.modules.ModuleServiceHandler;
import com.abocode.aichat.server.context.delivery.reply.Replies;
import com.alibaba.fastjson.JSON;

/**
 * @author guanxf
 */
public class PresenceSetServiceHandler implements ModuleServiceHandler {
    @Override
    public Replies execute(ChannelWrapper channelWrapper, MessageStanza stanza, ServerRuntimeContext serverRuntimeContext) {
        PresenceMessageBody body = JSON.parseObject(stanza.body().toString(),PresenceMessageBody.class);
        String presence = body.getPresence();
        String jid = body.getJid();
        Entity entity = EntityImpl.parse(jid);
        entity.setPresence(presence);
        serverRuntimeContext.getProtocolChannelRegistry().presence(entity);
        Replies replies = new Replies();
        replies.addReply(channelWrapper.getChannel(), StanzaWrapper.wrapperSuccess(stanza));
        return replies;
    }

    @Override
    public ModuleServiceType moduleServiceType() {
        return ModuleServiceType.PresenceSet;
    }
}
